/*
 * @copyright 2010 Gerald Jacobson
 * @license GNU General Public License
 * 
 * This file is part of Descent Assistant.
 *
 * Descent Assistant is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Descent Assistant is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Descent Assistant.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.amphiprion.descenttool;

import java.io.Serializable;
import java.util.List;

import org.amphiprion.descenttool.adapter.AvailableMonsterAdapter;
import org.amphiprion.descenttool.dao.LevelDao;
import org.amphiprion.descenttool.dao.MonsterDao;
import org.amphiprion.descenttool.entity.AvailableMonster;
import org.amphiprion.descenttool.entity.Level;
import org.amphiprion.descenttool.entity.Monster;
import org.amphiprion.descenttool.task.AddMonsterTask;
import org.amphiprion.descenttool.task.AddMonsterTask.AddMonsterListener;
import org.amphiprion.descenttool.util.PreferenceUtil;
import org.amphiprion.descenttool.util.PreferenceUtil.Preference;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

/**
 * @author Amphiprion
 * 
 */
public class ChooseMonster extends Activity {
	private List<AvailableMonster> monsters;
	private String typeFilter = "A";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_monster);

		final EditText txtName = (EditText) findViewById(R.id.txtName);

		final ListView lstMonsters = (ListView) findViewById(R.id.lstMonsters);
		monsters = MonsterDao.getInstance().getMonsters("");
		final AvailableMonsterAdapter adapter = new AvailableMonsterAdapter(this, lstMonsters, monsters);
		lstMonsters.setAdapter(adapter);
		lstMonsters.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		txtName.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
				collectChecked(lstMonsters, adapter);
				adapter.getFilter().filter(typeFilter + "|" + txtName.getText().toString());
			}
		});

		final Spinner cbSpinner = (Spinner) findViewById(R.id.cbFilter);
		cbSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				collectChecked(lstMonsters, adapter);
				if (cbSpinner.getSelectedItemPosition() == 0) {
					typeFilter = "A";
				} else if (cbSpinner.getSelectedItemPosition() == 1) {
					typeFilter = "C";
				} else if (cbSpinner.getSelectedItemPosition() == 2) {
					typeFilter = "M";
				} else if (cbSpinner.getSelectedItemPosition() == 3) {
					typeFilter = "H";
				} else if (cbSpinner.getSelectedItemPosition() == 4) {
					typeFilter = "B";
				} else if (cbSpinner.getSelectedItemPosition() == 5) {
					typeFilter = "E";
				} else if (cbSpinner.getSelectedItemPosition() == 6) {
					typeFilter = "L";
				}
				adapter.getFilter().filter(typeFilter + "|" + txtName.getText().toString());

			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		final Button btOk = (Button) findViewById(R.id.btOk);
		btOk.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				collectChecked(lstMonsters, adapter);
				if (PreferenceUtil.getInt(Preference.ENCOUNTER_MODE, 0) == 0) {
					// FREE MODE
					final List<Level> levels = LevelDao.getInstance().getLevels();
					if (levels != null) {
						AlertDialog.Builder builder = new AlertDialog.Builder(ChooseMonster.this);
						// builder.setTitle(getResources().getString(R.string.app_name));
						builder.setAdapter(new ArrayAdapter<Level>(ChooseMonster.this, android.R.layout.simple_dropdown_item_1line, levels), new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int item) {
								String level = levels.get(item).id;

								dialog.dismiss();
								launchTask(level);
							}
						});
						AlertDialog alert = builder.create();
						alert.show();
					}
				} else {
					launchTask(null);
				}

			}
		});

	}

	private void launchTask(String level) {
		AddMonsterTask task = new AddMonsterTask(new AddMonsterListener() {

			@Override
			public void importEnded(boolean succeed, List<Monster> selectedMonsters) {
				if (succeed && selectedMonsters != null) {
					Intent intent = new Intent();
					intent.putExtra("MONSTERS", (Serializable) selectedMonsters);
					setResult(RESULT_OK, intent);
				} else {
					setResult(RESULT_CANCELED);
				}
				finish();

			}

			@Override
			public Context getContext() {
				return ChooseMonster.this;
			}
		}, monsters, level);
		task.execute();
	}

	private void collectChecked(ListView lstMonsters, AvailableMonsterAdapter adapter) {

		for (int i = 0; i < adapter.getCount(); i++) {
			adapter.getItem(i).checked = lstMonsters.isItemChecked(i);
		}
	}

}
